<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.yaodian100.core.common.utils.ServerValue" %>
<%@ page import="com.yaodian100.core.admin.utils.AdminHelper" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/taglibs.jsp" %>

<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath()%>' />

<!-- 数字验证 -->
<script type="text/javascript" charset="UTF-8" src="${midContextPath}/js/jquery.alphanumeric.js"></script>
<!-- 月历 -->
<script type="text/javascript" charset="UTF-8" src="${midContextPath}/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="${midContextPath}/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="${midContextPath}/css/smoothness.datepick.css" />

<script language="javascript">
	$().ready(function(){
		$('#rechargeDeadline').datepick({minDate: new Date(), showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
		
		$("input[name*='amount']").numeric({allow:"."});
		$("input[name*='count']").numeric();
		
		$("input[name*='amount']").blur(function(){
			if(isNaN(this.value)){
				alert("请输入数字");
				return false;
			}
			total();
		});
		
		$("input[name*='count']").blur(function(){
			total();
		});
		
		$("#addBtn").click(function(){
			var option='<br />'+
			'面额：<input type="text" name="amount" size="10" value="0" />\r\n'+
			'数量：<input type="text" name="count" size="10" value="0" />\r\n'+
			'小计：<input type="text" name="singleTotal" size="15" value="0" readonly="readonly" />';
			$("#addOption").append(option);
			
			$("input[name*='amount']:last").numeric({allow:"."});
			$("input[name*='count']:last").numeric();
			
			$("input[name*='amount']:last").blur(function(){
				if(isNaN(this.value)){
					alert("请输入数字");
					return false;
				}
				total();
			});
			
			$("input[name*='count']:last").blur(function(){
				total();
			});
		});
		
		$("#saveBtn").click(function(){
			if($("#giftcardName").val()==""){
				alert("请输入礼品卡名称");
				return false;
			}
			
			if($("#rechargeDeadline").val()==""){
				alert("请输入充值期限");
				return false;
			}
			
			if($("#applyUser").val()==""){
				alert("请输入申请人");
				return false;
			}
			
			if($("#total").val()<=0){
				alert("总金额为0，请确认各笔的面额、数量是否正确");
				return false;
			}
			
			$("#addForm").submit();
		});
	});
	
	function total(){
		var total=0;
		$("input[name*='singleTotal']").each(function(index){
			var amount=document.getElementsByName("amount")[index].value;
			var count=document.getElementsByName("count")[index].value;
			this.value=amount*count;
			total=total+(amount*count);
		});
		
		$("#total").attr("value",total);
	}
</script>

<h1>礼品卡管理 - 新增</h1>

<s:if test="hasActionErrors()">
	<s:actionerror cssClass="msg rn"/><p />
</s:if>
<s:if test="hasActionMessages()">
	<s:actionmessage cssClass="msg rn"/><p />
</s:if>
<s:if test="hasFieldErrors()">
	<s:fielderror cssClass="msg rn"/><p />
</s:if>

<div class="f-link">
	<a href="${midContextPath}/giftcard/list.do">回礼品卡管理查询列表页</a>
</div>

<s:form action="save.do" method="post" id="addForm">
	<table class="tb-data" width="100%">	
		<tr>
			<th>*礼品卡名称</th>
			<td>
				<input type="text" name="giftcardName" id="giftcardName" size="50" maxlength="50" />
				<span class="txt-rd">【※限50个中文字】</span>
			</td>
		</tr>
		<tr>
			<th>*充值期限</th>
			<td>
				<input type="text" name="rechargeDeadline" id="rechargeDeadline" size="10" maxlength="10" readonly="readonly" />
			</td>
		</tr>
		<tr>
			<th>*申请人</th>
			<td>
				<input type="text" name="applyUser" id="applyUser" size="10" maxlength="15" />
			</td>
		</tr>
		<tr>
			<th>建档人</th>
			<td>
				<%=AdminHelper.getUser().getUsername()%>
			</td>
		</tr>
		<tr>
			<th></th>
			<td id="addOption">
				总金额：<input type="text" name="total" id="total" size="15" value="0" readonly="readonly" /><br />
				面额：<input type="text" name="amount" size="10" value="0" />
				数量：<input type="text" name="count" size="10" value="0" />
				小计：<input type="text" name="singleTotal" size="15" value="0" readonly="readonly" />
				<input type="button" name="addBtn" id="addBtn" value="新增" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="button" name="saveBtn" id="saveBtn" value="储存" />
				<input type="reset" value="复位" />
			</td>
		</tr>
	</table>
</s:form>
<br />
</table>